package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.LearningExperienceDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.LearningExperience;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLLearningExperienceInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.LearningExperienceInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddLearningExperienceMutation implements DataFetcher {

    @Autowired
    LearningExperienceDao learningExperienceDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加学习经历
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLLearningExperienceInput input = LearningExperienceInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        LearningExperience learningExperience = null;

        if (input.getId() != null) {
            learningExperience = learningExperienceDao.get(LearningExperience.class, input.getId());
            learningExperience.setUpdatedAt(new Date());
        } else {
            learningExperience = new LearningExperience();
            learningExperience.setCreatedAt(new Date());
        }
        if(schoolId != null){

            learningExperience.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            learningExperience.setTeacher_id(input.getTeacher_id());
        }
        if(input.getLearnlevel() != null){

            learningExperience.setLearnlevel(input.getLearnlevel());
        }
        if(input.getStartdate() != null){

            learningExperience.setStartdate(input.getStartdate());
        }
        if(input.getEnddate() != null){

            learningExperience.setEnddate(input.getEnddate());
        }
        if(input.getWay() != null){

            learningExperience.setWay(input.getWay());
        }
        if(input.getDegree() != null){

            learningExperience.setDegree(input.getDegree());
        }
        if(input.getImage() != null){

            learningExperience.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            learningExperience.setShenhe(input.getShenhe());
        }
        if(input.getType() != null){

            learningExperience.setType(input.getType());
        }
        if(input.getAcademy() != null){

            learningExperience.setAcademy(JSON.toJSONString(input.getAcademy()));
        }
        if(input.getProfession() != null){

            learningExperience.setProfession(JSON.toJSONString(input.getProfession()));
        }
        if(input.getWorkstationname() != null){

            learningExperience.setWorkstationname(JSON.toJSONString(input.getWorkstationname()));
        }
        if(input.getResearche() != null){

            learningExperience.setResearche(JSON.toJSONString(input.getResearche()));
        }
        if(input.getDuration() != null){

            learningExperience.setDuration(input.getDuration());
        }

        try{
            learningExperienceDao.saveOrUpdate(learningExperience);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

